#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cstring>

int main(int argc, char *argv[])
{
	//创建套接字
	int sock_fd;
	sock_fd = socket(AF_INET,SOCK_STREAM,0);
	//连接服务器
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(6666);
	inet_pton(AF_INET,"192.168.0.112",&addr.sin_addr.s_addr);
	int ret = connect(sock_fd,(struct sockaddr *)&addr,sizeof(addr));
	printf("ret=%d\n",ret);
	//读写数据
	char buf[1024]="";
	while(1)
	{
		printf("请输入数据\n");
		memset(buf,0,sizeof buf);
		int n = read(STDIN_FILENO,buf,sizeof(buf));
		write(sock_fd,buf,n);//发送数据给服务器
		printf("send:%s\n",buf);
		memset(buf,0,sizeof buf);
		n = read(sock_fd,buf,sizeof(buf));
		printf("nread=%d recv:%s\n",n,buf);
	
	}

	
	
//关闭
	close(sock_fd);

	return 0;
}
